#!/bin/bash
##iozone v3_430
test_iozone(){
	if [[ -e '/usr/bin/iozone' ]];then
		if [[ "$mode" == "auto" ]];then
			echo -e "使用默认参数"
			if [[ -z "$iozone_test_path" ]];then
				iozone_test_path=/data/iozone
			fi
			if [[ -z "$iozone_test_sum" ]];then
				iozone_test_sum=1
			fi
		else
			if [[ -z "$iozone_test_sum" ]];then
				input "请输入测试次数,默认[ 1 ]" iozone_test_sum
			fi
			if [[ -z "$iozone_test_sum" ]];then
				input "请输入需要测试的硬盘挂载目录,格式：[ /ssd/iozone/ ],默认[ ${iozone_test_path} ]" iozone_test_sum
			fi
		fi
		input_check "$iozone_test_sum" "[ iozone ]测试次数" 1 iozone_test_sum
		input_check "$disk" "硬盘挂载目录" "${iozone_test_path}" disk

		# 开始创建测试文件夹
		mkdir -p $disk 
		
		# 切换工作目录到创建的文件夹
		pushd $disk
		mkdir iozone
		pushd iozone
		iozone_test_path=`pwd`
		# 删除日志文件(如果存在)
		rm -rf $iozone_log_dir
		mkdir -p ${iozone_test_path}
		mkdir -p ${iozone_log_dir}
		# 开始检测
		green_zt '正在检测本机内存...'
		if [[ -z "$Mem_size" ]];then
			ram=`free -g | awk '{print $2}' | awk 'NR==2{print}'`
			ram=`expr $ram + 1`
		else
			ram=$Mem_size
		fi
		red_z_green_z "本机内存:" "[ ${ram}G ]"
		green_zt "正在检测[ ${iozone_test_path} ]所在磁盘空间"
		rom=` df -h ${iozone_test_path} | awk 'NR==2{print}' | awk '{print $4}' |sed 's/G//'`
		nuber="`expr $ram \* 2`"
		red_z_green_z "指定测试空间剩余: " "[ ${rom}G ]"

		# 开始判断测试条件
		if [[ "$rom" -le "$nuber" ]];then
			red_bg '磁盘空间不足,请更换测试目录或尝试清理该目录'
		else
			red_z_green_z "本机内存:" "[ ${ram}G ]"
			red_z_green_z "设置块大小：" "[ 16M ]"
			rams="`expr $ram / 2` $ram `expr $ram \* 2`"
			for i in $rams;do
				for((c=1;c<=${iozone_test_sum};c++));do
					red_z_green_z "正在进行的内存大小----->" "[ ${i}G ]"
					red_z_green_z "当前大小测试次数:------>" "[ $c ]"
					red_z_green_z "如需终止请执行--------->" "[ sudo kill -9 $$ ]"
					red_z_green_z "执行的命令--->" "[ iozone -a -i 0 -i 1 -i 2 -f  ${iozone_test_path}/${i}G_${c}.file -r 16M -s ${i}G | tee -a ${iozone_log_dir}/${i}G_${c}.log ]"
					iozone -a -i 0 -i 1 -i 2 -f  ${iozone_test_path}/${i}G_${c}.file -r 16M -s ${i}G | tee -a ${iozone_log_dir}/${i}G_${c}.log
					rm -f ${iozone_test_path}/${i}G_${c}.file
				done
			done
			red_z_green_z "测试完成,结果保存--->" "[ ${iozone_log_dir} ]"
		fi
	else
		red_z_green_z "请先安装iozone,并创建软连接至----->" "[ /usr/bin/iozone ]"
	fi
}
if [[ "$1" == 'f' ]];then
    if [[ -e './item.sh' ]];then
        source ./item.sh
		test_iozone
    else
        echo -e "请进入目录:[ spzx/ ]再执行脚本"
    fi
else
    if [[ -z "$1" ]];then
		echo -e "请在脚本执行的后面加上一个参数直接进行测试，例如--->" "[ bash spzx/test/iozone.sh f ]"
    fi
fi